$(function () {
	
	function HTMLEncode(html) {
		var temp = document.createElement("div");
		(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
		var output = temp.innerHTML;
		temp = null;
		return output;
	}
	
	function HTMLDecode(text) { 
		var temp = document.createElement("div"); 
		temp.innerHTML = text; 
		var output = temp.innerText || temp.textContent; 
		temp = null; 
		return output; 
	} 
	
	var editer = UE.getEditor('container');
	$('#save').bind("click",function(){
		
		var content = editer.getContent();
		console.log(content);
		var lcId = $('#js-selected').val();
		 $.post(baseURL + '/app/lc/save',{'id':lcId,'html':HTMLEncode(content)},function(result){
			 alert(result.msg);
		 });
		
	})

	
	var $selected = $('#js-selected');
	$.get(baseURL + '/app/lc/list', function(data) {
		$.each(data.list, function(index, item) {
			$selected.append('<option value="' + item.id + '">'+item.name+'</option>');
		});
	});
	
	$('#js-selected').bind("change", function() {
		if($(this).val() == ''){
			return;
		}
		$.get(baseURL + 'app/lc/info/'+$(this).val(), function(data) {
			if(data.code == 500){
				editer.setContent('', '');
				alert(data.msg);
				return;
			}
			console.log(data.lcTemplate.html);
			editer.setContent(HTMLDecode(data.lcTemplate.html), '');
		});
		
	})
    $("#jqGrid").jqGrid({
        url: baseURL + 'app/pdf/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id', width: 25, key: true ,hidden : true},
			{ label: '名称', name: 'name', sortable: false, width: 75 }
			//{ label: 'pdf路径', name: 'pathPdf', width: 100 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            paramKey: null
		},
		showList: true,
		showPdf: false,
		title: null,
		config: {},
		pdf: null
	},
	methods: {
		viewPdf: function(){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			$.get(baseURL + "app/pdf/info",{"id":id}, function(r){
				vm.showList = false;
				vm.showPdf = true;
				//$('#a_pdf').attr("href","http://pdf.chenwenxi.cc/bx/"+r.pdfLog.name);
				window.location.href = "http://"+r.fastdfs+"/"+r.pdfInfo.path;
            });
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.config = {};
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			$.get(baseURL + "app/pdf/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.config = r.config;
            });
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/config/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function (event) {
			var url = vm.config.id == null ? "sys/config/save" : "sys/config/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.config),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			vm.showPdf = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'paramKey': vm.q.paramKey},
                page:page
            }).trigger("reloadGrid");
		}
	}
});